package com.hnevc.androidui8;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    //申明控件成员变量
    EditText mEtUserName;
    EditText mEtPassword;
    Button mBtnRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件，保存到变量中
        mEtUserName  = findViewById(R.id.et_username);
        mEtPassword = findViewById(R.id.et_password);
        mBtnRegist = findViewById(R.id.btn_regist);

        //设置控件的事件
        mBtnRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存用户名和密码到内部文件中data.txt

                try {
                    //获取输出流
                    FileOutputStream fos = openFileOutput("data.txt",MODE_PRIVATE);
                    //获取用户输入的数据，构造字符串
                    String data = mEtUserName.getText().toString()
                            +"-"
                            +mEtPassword.getText().toString();
                    //讲字符串字节数组，写到输出流
                    fos.write(data.getBytes());
                    //关闭流
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent(MainActivity.this,Activity02.class);
                startActivity(intent);
            }
        });

    }
}
